/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session.interceptor;


import javax.servlet.http.HttpServletResponse;

import org.independent.servlet.session.manager.SessionsManager;
import org.independent.servlet.session.service.ContextHolderFactory;

/**
 * intercept the method of HttpServletResponse's encodeURL and encodeRedirectURL
 * 
 * @see javax.servlet.http.HttpServletResponse.encodeURL(String)
 * @see javax.servlet.http.HttpServletResponse.encodeRedirectURL(String)
 * 
 * @author lcj
 */
public aspect ResponseURLInterceptor {
	static private String sessionIdName = SessionsManager.sessionIdName;
	/**
	 * the name of some webserver's session,is default by web server self so it
	 * must be extracted out
	 */
	static private String webServerSessionIdName = "";

	pointcut addSessionIdByURL(HttpServletResponse httpServletResponse,
			String URL):(execution(public String javax.servlet.http.HttpServletResponse.encodeURL(String))||call(public String javax.servlet.http.HttpServletResponse.encodeURL(String))||execution(public String javax.servlet.http.HttpServletResponse.encodeRedirectURL(String))||call(public String javax.servlet.http.HttpServletResponse.encodeRedirectURL(String)))&&target(httpServletResponse)&&args(URL);

	String around(HttpServletResponse httpServletResponse, String URL)  :addSessionIdByURL(httpServletResponse, URL){
		String url = proceed(httpServletResponse, URL);
		/**
		 * only when the request is not support the cookie,the session's id will
		 * be send by url
		 */
		if (ContextHolderFactory.getServiceContextHolder().getContext()
				.getSessionIdCommitType() == 1) {

			/**
			 * only the url has the session's id then the url will be rewriter
			 */
			if (url.indexOf("@") > -1) {
				if (webServerSessionIdName.equals("")) {
					int semicolonPosition = url.indexOf(";");
					int sessionIdPosition = url.substring(semicolonPosition)
							.indexOf("=");
					webServerSessionIdName = url.substring(
							semicolonPosition + 1, sessionIdPosition);
				}
				int sessionIdposition1 = url.indexOf(";"
						+ webServerSessionIdName + "=");
				int idMarkLength = webServerSessionIdName.length();
				String sessionId = url.substring(sessionIdposition1
						+ idMarkLength + 2, url.length());
				sessionId = SessionsManager.getInstance().formatSessionId(
						sessionId);
				return (url.substring(0, sessionIdposition1 + 1)
						+ sessionIdName + "=" + sessionId);
			}
		}
		return url;
	}
}